package com.jin.flashsaled.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jin.flashsaled.RabbitMQ.RabbitSend;
import com.jin.flashsaled.entity.Order;
import com.jin.flashsaled.entity.SeckillGoods;

import com.jin.flashsaled.entity.SeckillOrder;
import com.jin.flashsaled.entity.User;
import com.jin.flashsaled.mapper.SeckillGoodsMapper;
import com.jin.flashsaled.result.ResultEnum;
import com.jin.flashsaled.service.IGoodsService;
import com.jin.flashsaled.service.IOrderService;
import com.jin.flashsaled.service.ISeckillGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 靳鹏宇
 * @since 2025-09-18
 */
@Service
@Slf4j
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService, InitializingBean {
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    IOrderService orderService;

    private Map<Long,Boolean> isHave=new HashMap<>();


    @Override
    public boolean haveStock(Long goodsId) {


//        SeckillGoods seckillGoods = seckillGoodsMapper.selectById(goodsId);
//        boolean isHave=seckillGoods.getStockCount()>0;

        //把从数据库中拿库存，改为从redis中拿库存
        boolean flag = false;
        //判断内存标记，减少库存为0后与redis的连接压力
        if (!isHave.get(goodsId))
        {
            return flag;

        }
        int  stockCount=Integer.parseInt(String.valueOf(redisTemplate.opsForValue().get("seckillGoods:" + goodsId)));
        if(stockCount>0)
        {
            flag=true;
            //自减1
            redisTemplate.opsForValue().decrement("seckillGoods:" + goodsId);

        }
        else
        {
            isHave.put(goodsId,false);
            flag=false;
        }



        return flag;
    }

    @Override
    public String getResult(User user, Model model, Long goodsId) {

        //查询redis有没有订单。
        SeckillOrder seckillOrder=(SeckillOrder) redisTemplate.opsForValue().get("seckillOrder:"+user.getId()+goodsId);
        if(seckillOrder==null)
        {
            model.addAttribute("error", ResultEnum.BUYING.getMsg());
            model.addAttribute("user",user);
            model.addAttribute("goods",goodsService.getGoodsWithDetailById(goodsId));
            return "goodDetail";

        }
        Order order=orderService.getBaseMapper().selectById(seckillOrder.getOrderId());
        model.addAttribute("order",order);

        return "orderDetail";
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        //在bean实例化完成后，进行的操作
        //我们将数据库库存缓存到redis中，方便后续做库存预扣
        LambdaQueryWrapper<SeckillGoods> wrapper = new LambdaQueryWrapper();
        wrapper.select(SeckillGoods::getGoodsId,SeckillGoods::getStockCount);
        List<SeckillGoods> list=seckillGoodsMapper.selectList(wrapper);
        //将秒杀商品表的库存放入redis
        for (SeckillGoods seckillGoods : list)
        {

            redisTemplate.opsForValue().set("seckillGoods:"+seckillGoods.getGoodsId(),seckillGoods.getStockCount());
            isHave.put(seckillGoods.getGoodsId(),true);
        }
    }
}
